<?php
session_cache_limiter('none');
session_name( 'NVBadmin' );
session_start();

error_reporting  (E_ERROR | E_WARNING | E_PARSE);
//error_reporting (E_ALL);
define('_VALID_NVB','1');
require( "../initcms.php" );

if (@$_GET['act']=="logout") 
{
  session_unset();
  session_destroy();
  redir("admin.php");
}
if (@$_POST['login'] )
{
	//if($_SESSION['imagesercurityadmin']==$_POST['imagesercurity']){
		
		$username = preg_replace("/[^a-zA-Z0-9_-\s]/", "", compile_post('username'));
		$password = compile_post('password');
		if ($username && $password)
		{
			$password=md5($password);
			$result = $DB->query("select * from users where username='$username' and password='$password'");		
			if(mysql_num_rows($result)>0) 
			{
				$a=mysql_fetch_array($result);
				$my['username']=$username;
				$my['lastvisit']="&#272;&#259;ng nh&#7853;p l&#7847;n g&#7847;n &#273;&#7845;y nh&#7845;t: ".date('d/M/y',$a['lastvisit']);
				$my['id']=$a['id_users'];
				$my['name']=$a['name'];
				$my['password']=$a['password'];
				if ($a['super']==1)
				{
					$my['usertype']="super";
				}
				else
				{
					$my['usertype']="normal";
				}
				$logintime = time();
				$_SESSION["session_username"] = $my['username'];
			
				$_SESSION["session_usertype"] = $my['usertype'];				
				$_SESSION["session_user_id"] = $my['id'];
				$_SESSION["session_logintime"] = $logintime;
				$_SESSION["addminname"]=$my['name'];
				$_SESSION["phone"]=$a['telephone'];
				$_SESSION["email"]=$a['email'];
				$_SESSION['password'] = $my['password'];
				
				session_write_close();
				
				$sql="update users set lastvisit='".$logintime."' where id_users=".$my['id'];
				mysql_query($sql);		
				//chuyen toi trang main.php
				echo '
					<html>
					<head>
					<title>Khu v&#7921;c qu&#7843;n tr&#7883;</title>
					</head>
					<frameset rows="64">
						<frame name="main" src="main.php">
					  <noframes>
					  <body>
					  <p>This page uses frames, but your browser doesn not support them.</p>					
					  </body>
					  </noframes>
					</frameset>
					</html>			
				';
				//redir("main.php");
				die();
			} 
			else 
			{
				echo "<script>alert('Incorrect Username and Password, please try again'); document.location.href='admin.php';</script>\n";
				session_unset();
				@session_destroy();			
				exit();
			}
		}
		else
		{
			echo "<script>alert('Please enter your username and password'); document.location.href='admin.php';</script>\n";
			session_unset();
			@session_destroy();			
			exit();	
		}
	/*}else{
		echo "<script>alert('Mã bảo vệ không đúng'); document.location.href='admin.php';</script>\n";
		session_unset();
			@session_destroy();			
			exit();	
	}*/

}
else{

?>
<html>
<head>
<title>&#272;&#259;ng nh&#7853;p qu&#7843;n tr&#7883;</title>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
</HEAD>
<link href="css/loginform.css" rel="stylesheet" type="text/css" />
</head>

<script language="javascript" src="js/jquery.min.js"></script>


<body  style="background:#ccc">




<form action="?" method="post" onSubmit="return check_null_login_binh();">
<input type="hidden" value="1" name="login">
<div id="loginform" style="margin-left:35%">
	<div id="titlelogin">Đăng nhập quản trị</div>
    <div style="clear:both; height:25px;"></div>
    <div id="boxlogin">
    <div style=" height:25px; padding-top:15px;">
    	<div style="float:left; padding-top:3px;"><strong>Mã đăng nhập:</strong></div>
        <div style="float:right; padding-right:10px;"><input class="textbox" name="username" id="username" type="text" /></div>
        <div style="clear:both"></div>
        </div>
    </div>
     <div style="clear:both; height:25px;"></div>
    <div id="boxlogin">
    	<div style=" height:25px; padding-top:15px;">
    	<div style="float:left; padding-top:3px;"><strong>Mật khẩu:</strong></div>
        <div style="float:right;  padding-right:10px;"><input class="textbox" id="password" name="password" type="password" /></div>
        </div>
    </div>
    <div style="clear:both; height:25px;"></div>
    <div id="boxlogin">
        <div style=" text-align:center">
        <div style="padding-top:10px;">
            <input name="" type="submit" id="btnlogin1" value="Đăng nhập"/> <input name="" type="reset" id="btnlogin" value="Nhập lại"/>
            </div>
        </div>
  </div>
   <div style="clear:both; height:10px;"></div>
   		<div align="center"><a href="#">Quên mật khẩu</a></div>
     <div style="clear:both; height:10px;"></div>
    
</div>
</form>
<script language="javascript">

function check_null_login_binh(){
	if($('#username').val()==''){
		alert('Bạn phải nhập vào tên đăng nhập !');
		$('#username').focus();
		return false;
	}
	if($('#password').val()==''){
		alert('Bạn phải nhập vào mật khẩu đăng nhập !');
		$('#password').focus();
		return false;
	}
}
</script>

</body>
</html>

<?php 
} 

require( "../endcms.php" );
?>